package cn.acewill.yxd.yunxundian.ui.user;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.LinkedList;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.utils.PinchImageView;

/**
 * Created by XXX on 2017/9/5.
 */

public class ImageViewPagerActivity extends BaseActivity implements ViewPager.OnPageChangeListener{
    private String[] fileString;
    private ImageView[] tips;
    private  ViewGroup group;
//    private View mBackground;
//    private   Rect rect;
//    private RectF mThumbRect;
//    //time
//    private long ANIM_TIME=300;

    private ObjectAnimator mBackgroundAnimator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photoview_page);
        String array=getIntent().getStringExtra("filepath");
        //默认第一个选中
        int selectId = getIntent().getIntExtra("position", 0);
        //获取上个视图的rect
 //       rect = getIntent().getParcelableExtra("rect");
        //去掉最后一个逗号
        final LinkedList<PinchImageView> viewCach=new LinkedList<>();
        if (array.contains(",")){
            fileString=array.substring(0,array.length()-1).split(",");
        }else{ //只有一个
            fileString=new String[1];
            fileString[0]=array;
        }
        group = (ViewGroup)findViewById(R.id.viewGroup);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);

        initDot();
        viewPager.addOnPageChangeListener(this);
         viewPager.setAdapter(new PagerAdapter() {
             @Override
             public int getCount() {
                 return fileString.length;
             }

             @Override
             public boolean isViewFromObject(View view, Object object) {
                 return view==object;
             }

             @Override
             public Object instantiateItem(ViewGroup container, int position) {
                 PinchImageView pv;
                 if (viewCach!=null && viewCach.size()>0){
                     pv=viewCach.remove();
                     pv.reset();
                 }else{
                     pv=new PinchImageView(ImageViewPagerActivity.this);
                 }
                 Glide.with(ImageViewPagerActivity.this).load(fileString[position]).thumbnail(0.1f).diskCacheStrategy(DiskCacheStrategy.ALL).into(pv);
                 container.addView(pv);
                 return pv;
             }

             @Override
             public void destroyItem(ViewGroup container, int position, Object object) {
                 PinchImageView pv = (PinchImageView) object;
                 container.removeView(pv);
                 viewCach.add(pv);
             }

             @Override
             public void setPrimaryItem(ViewGroup container, int position, Object object) {
                 PinchImageView pv= (PinchImageView) object;
                 Glide.with(ImageViewPagerActivity.this).load(fileString[position]).diskCacheStrategy(DiskCacheStrategy.ALL).into(pv);
             }
         });
        //在setadpater之后调用
        viewPager.setCurrentItem(selectId);
    }

//    public void thubView(){
//        mBackground.post(new Runnable() {
//            @Override
//            public void run() {
//                mBackground.setAlpha(1f);
//                //背景动画
//                mBackgroundAnimator=ObjectAnimator.ofFloat(viewPager,"alpha",0f,1f);
//                mBackgroundAnimator.setDuration(ANIM_TIME);
//                mBackgroundAnimator.start();
//
//                Rect tempRect = new Rect();
//                viewPager.getGlobalVisibleRect(tempRect);
//                rect.top = rect.top-tempRect.top;
//                rect.bottom = rect.bottom-tempRect.top;
//                //mask动画
//                mThumbRect=new RectF();
//                RectF bigMaskRect = new RectF(0,0,viewPager.getWidth(),viewPager.getHeight());
//
//            }
//        });
//    }

    public void initDot(){
        tips = new ImageView[fileString.length];
        for(int i=0; i<tips.length; i++){
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20,20));
            //    imageView.setPadding(10,0,10,0);
            tips[i] = imageView;
            if(i == 0){
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            }else{
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 10;
            layoutParams.rightMargin = 10;
            group.addView(imageView,layoutParams);
        }
    }


    //设置点点
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setImageBackground(position % fileString.length);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    /**
     * 设置选中的tip的背景
     * @param selectItems
     */
    private void setImageBackground(int selectItems){
        for(int i=0; i<tips.length; i++){
            if(i == selectItems){
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            }else{
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
        }
    }



}
